<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;

/**
 * Todaycount控制器
 */
class Todaycount extends AdminBase
{

    protected $model = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Todaycount();

        $this->validate = \app\admin\validate\Todaycount::class;
    }

    public function index()
    {
        list($limit, $where, $sortArr) = $this->buildTableParames();
        $start_time = 0;
        $end_time = 0;

        // $post = $this->request->post();
        $day_changes = $this->request->get('day_changes');
        $start_time = $this->request->get('start_time_new');
        $end_time = $this->request->get('end_time_new');
        if (!empty($start_time) && !empty($end_time)) {
            $start_time = strtotime(date('Y-m-d 00:00:00', $start_time));
            if ($day_changes == 3) {
                $start_time = strtotime(date('Y-m-1 00:00:00', $start_time));
            }
            $start_time_str = date('Y-m-d H:i:s', $start_time);
            if ($day_changes == 101) {
                $end_time = strtotime(date('Y-m-d 23:59:59', $end_time));
            } else {
                $end_time = strtotime(date('Y-m-d 00:00:00', $end_time));
            }
            $end_time_str =  date('Y-m-d H:i:s', $end_time);

            $where[] = ['order_time', '>=', $start_time_str];
            $where[] = ['order_time', '<=', $end_time_str];
        }

        if (!empty($where)) {
            foreach ($where as $k => $v) {
                if ($v['0'] == 'order_time' && $v['1'] == '>=') {
                    $start_time = $v['2'];
                }
                if ($v['0'] == 'order_time' && $v['1'] == '<=') {
                    $where[$k]['2'] = date('Y-m-d 23:59:59', strtotime($v['2']));
                    $end_time = $where[$k]['2'];
                }
            }
        }

        //默认筛选本月一号到现在
        if (empty($where)) {
            $where[] = ['order_time', '>=', date('Y-m-1 00:00:00', time())];
            $where[] = ['order_time', '<=', date('Y-m-d 00:00:00', time())];
        }

        $list = $this->model
            ->where($where)
            ->field("brand,sku,SUM(`count`) as count,order_time,product_id")
            ->order('count desc')
            ->group('brand')
            ->paginate($limit);
        $lists = $list->items();
        //查询产品信息表中的图片信息  编号作为键 图一作为值
        $images = table('product_info')->select()->toarray();
        $images = array_column($images, 'pic1image', 'brand');

        foreach ($lists as $k => $v) {
            $lists[$k]['count'] = $v['count'];
            //图片
            $order_product = table('product')
                ->where('id', $v['product_id'])
                ->find();
            $lists[$k]['images'] = isset($images[$v['brand']]) ? $images[$v['brand']] : '';
            // if (!empty($order_product)) {
            //     $lists[$k]['images'] = $order_product['images'];
            // } else {
            //     $lists[$k]['images'] = '';
            // }
            //最新成本
            $procurement_cost = table('procurement_cost')
                ->where('number', $v['brand'])
                ->order('month desc')
                ->find();
            if (!empty($procurement_cost)) {
                $lists[$k]['material'] = $procurement_cost['material'];
            } else {
                $lists[$k]['material'] = '';
            }
        }

        $data = [
            'code' => 1,
            'msg' => '',
            'count' => $list->total(),
            'data' => $lists,
            'start_time' => $start_time,
            'end_time' => $end_time,
        ];
        return json($data);
    }


    /**
     * 查找方法
     */
    public function find()
    {
        $post = $this->request->post();
        if (empty($post['brand'])) {
            return error('网络错误请稍后再试');
        }
        $where['brand'] = $post['brand'];
        if (!empty($post['start_time']) && !empty($post['end_time'])) {
            $where[] = ['order_time', '>=', $post['start_time']];
            $where[] = ['order_time', '<=', $post['end_time']];
        } else {
            //默认筛选本月一号到现在
            $where[] = ['order_time', '>=', date('Y-m-1 00:00:00', time())];
            $where[] = ['order_time', '<=', date('Y-m-d 00:00:00', time())];
        }
        $result  = $this->model->where($where)->select()->toarray();
        $data = [
            'code' => 1,
            'msg' => '',
            'data' => $result,
        ];
        return json($data);
    }
}
